package com.tupu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.Result;
import com.tupu.entity.admin.LogInfo;
import com.tupu.service.admin.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/log")
public class LogController {

    @Autowired(required = false)
    private LogService logService;

    /**
     * 添加日志
     *
     * @param logInfo
     * @return
     */
    @PostMapping("/save")
    public Result<LogInfo> save(@RequestBody(required = false) LogInfo logInfo) {
        try {
            logService.addLog(logInfo);
            return Result.ok(logInfo);
        } catch (Exception e) {
            return Result.errorMessage("新增日志失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 根据id删除日志
     *
     * @param logId
     * @return
     */
    @PostMapping("/del")
    public Result delById(@RequestParam(required = false) Integer logId) {
        try {
            logService.deleteById(logId);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage("删除失败,错误信息:" + e.getMessage());
        }
    }

    /**
     * 查询日志
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("/searchList")
    public Result<Page<LogInfo>> searchByName(@RequestParam(required = false, defaultValue = "1") Long page,
                                              @RequestParam(required = false, defaultValue = "10") Long size,
                                              @RequestParam(required = false) String name) {
        try {
            Page<LogInfo> pageInfo = logService.pageInfo(name, page, size);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.errorMessage("错误信息:" + e.getMessage());
        }
    }

    /**
     * 日志列表 分页 模糊搜索
     *
     * @param page
     * @param size
     * @param user_name
     * @return
     */
    @PostMapping("/list")
    public Result findByPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                             @RequestParam(required = false, defaultValue = "10") Integer size,
                             @RequestParam(required = false) String user_name) {
        try {
            Page<LogInfo> pageInfo = logService.pageResult(page, size, user_name);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 日志列表 不分页(批量导出数据)
     *
     * @param user_name
     * @return
     */
    @PostMapping("/listAll")
    public Result findAll(@RequestParam(required = false) String user_name) {
        try {
            List<LogInfo> logInfos = logService.findAll(user_name);
            return Result.ok(logInfos);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }
}